#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>

/* /etc/services */

static void
show_service(const struct servent *service) {
    printf("%10s  %s\n", "name", service->s_name);
    for (int i = 0 ;service->s_aliases[i] != NULL; i++) {
        printf("%10s  %s\n", "alias", service->s_aliases[i]);
    }

    printf("%10s  %d\n", "port", ntohs(service->s_port));
    printf("%10s  %s\n", "protocol", service->s_proto);
}

int
main(int argc, char** argv) {
    if (argc < 2) {
        fprintf(stderr,"Usage: %s ([service names] [proto])+", argv[0]);
        exit(-1);
    }
    for (int i = 1 ; i+1 < argc ; i += 2) {
        struct servent* service = getservbyname(argv[i], argv[i+1]);
        if (service == NULL) {
            fprintf(stderr, "error get info of service(%s) with proto(%s)",
                    argv[i], argv[i+1]);
            continue;
        }
        show_service(service);
        puts("");
    }

    return 0;
}
